/*
 * Copyright 2012-2014 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.jqd.framework.statistics;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Map;

import com.jqd.framework.util.Assert;
import com.jqd.framework.util.IoUtils;

/**
 * 代码统计功能入口类
 * TODO 目前只能统计java代码量，考虑加入统计js,jsp,html文件的代码量
 * 
 * @author liaozhicheng.cn@gmail.com
 * @date 2012-8-10
 * @since 1.0
 */
public class StatisticsCodeMain {
	
	private static Map<String, File> sourceFileMap = new HashMap<String, File>();
	
	private static Long sourceCounter = 0L;
	
	private static Long commentCounter = 0L;
	
	private static Long blankCounter = 0L;
	
	
	public static void main(String[] args) {
		File project = new File("D:/workspace/work_trunk");
		filterSourceFiles(project, new JavaFileFilter());
		printFileMap();
		CodeSorter sorter = new JavaCodeSorter();
		for (Map.Entry<String, File> entry : sourceFileMap.entrySet()) {
			sortCode(entry.getValue(), sorter);
		}
		printResult();
	}
	
	
	private static void printResult() {
		System.out.println("effective code : " + sourceCounter);
		System.out.println("comment : " + commentCounter);
		System.out.println("blank : " + blankCounter);
		System.out.println("sum : " + (sourceCounter + commentCounter + blankCounter));
	}
	
	
	private static void sortCode(File source, CodeSorter sorter) {
		Assert.notNull(source);
		
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(source));
			String lineCode = null;
			while ((lineCode = br.readLine()) != null) {
				CodeType type = sorter.sort(lineCode);
				switch (type) {
					case SOURCE:
						sourceCounter++;
						break;
					case COMMENT:
						commentCounter++;
						break;
					case BLANKLINE:
						blankCounter++;
						break;
					default:
						throw new IllegalArgumentException("no define type : " + type);
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			IoUtils.closeSilently(br);
		}
	}
	
	
	private static void filterSourceFiles(File project, FileFilter filter) {
		Assert.notNull(project);
		File[] rootFiles = project.listFiles(filter);
		for (File rootFile : rootFiles) {
			if (rootFile.isDirectory()) {
				filterSourceFiles(rootFile, filter);
			}
			else {
				sourceFileMap.put(rootFile.getAbsolutePath(), rootFile);
			}
		}
	}
	
	
	private static void printFileMap() {
		// for (Map.Entry<String, File> entry : sourceFileMap.entrySet()) {
		// System.out.println(entry.getKey() + " : " + entry.getValue().getName());
		// }
		System.out.println("total file : " + sourceFileMap.size());
	}
	
}
